# -*- coding: utf-8 -

import time
import datetime
import urllib.request, json
import Core.MongoDB as MongoDB
import Core.Gadget as Gadget
import numpy as np
import random


def getPoFriends(stockcode, cookie):
    headers = {}
    headers['X-Requested-With'] = 'XMLHttpRequest'
    headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0'
    headers['Host'] = 'xueqiu.com'
    headers['Connection'] = 'keep-alive'
    headers['Accept'] = '*/*'
    headers['Referer'] = 'https://xueqiu.com/S/' + stockcode + '/follows'
    headers['Cookie'] = 'device_id=fad72f795742c25d718dde99220a0b7d; s=f51i83xerj; bid=2c6a878a053a35ed2656218da5ae29be_jc8cz4kx; __utmz=1.1523948595.65.4.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; _ga=GA1.2.942722972.1515475000; aliyungf_tc=AQAAAK9etEO/NA8AGBfIb9LXzku/nDKH; Hm_lvt_1db88642e346389874251b5a1eded6e3=1537425206,1537499018,1537854240,1538957259; _gid=GA1.2.1413757261.1538957260; _gat_gtag_UA_16079156_4=1; __utma=1.942722972.1515475000.1537499019.1538957261.113; __utmc=1; __utmt=1; remember=1; remember.sig=K4F3faYzmVuqC0iXIERCQf55g2Y; xq_a_token=bac8e85c6e5546899703d83d4dbbc7a850644b17; xq_a_token.sig=_uufy5ObX9-ZcJIqCjvqjNAkMFY; xqat=bac8e85c6e5546899703d83d4dbbc7a850644b17; xqat.sig=yXpjV_WrUTNx3Qb2siG8WI5cVC0; xq_r_token=ed17b066bfb02fc424e072af82c5ad1f697b5c45; xq_r_token.sig=uAjUM3y3XlwWmxu1wxCipdcoy8o; xq_is_login=1; xq_is_login.sig=J3LxgPVPUzbBg3Kee_PquUfih7Q; u=4653766544; u.sig=l0rHkleae2sm4Ph3sS6R0e5K7f0; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1538957274; __utmb=1.2.10.1538957261'

    url = 'https://xueqiu.com/recommend/pofriends.json?type=1&code=' + stockcode
    req = urllib.request.Request(url, headers=headers)
    a = urllib.request.urlopen(req)
    b = a.read()
    html = json.loads(b.decode('utf-8'))

    if html['totalcount']:
        f = html['totalcount']
        tday = time.strftime('%Y%m%d', time.localtime(time.time()))
        # print(tday,stockcode,f)
        return (tday, stockcode, f)


def StockList(database):
    trades = database.find("Instruments", "Stock")
    symbols = []
    for trade in trades:
        symbol = trade["Symbol"]
        symbols.append(symbol)
    return symbols


def Data_Xueqiu_followers(symbol):
    a = random.randint(0, 1)
    # print(a)
    cookie1 = 'device_id=fad72f795742c25d718dde99220a0b7d; s=f51i83xerj; bid=2c6a878a053a35ed2656218da5ae29be_jc8cz4kx; __utmz=1.1523948595.65.4.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; _ga=GA1.2.942722972.1515475000; aliyungf_tc=AQAAAK9etEO/NA8AGBfIb9LXzku/nDKH; Hm_lvt_1db88642e346389874251b5a1eded6e3=1537425206,1537499018,1537854240,1538957259; _gid=GA1.2.1413757261.1538957260; _gat_gtag_UA_16079156_4=1; __utma=1.942722972.1515475000.1537499019.1538957261.113; __utmc=1; __utmt=1; remember=1; remember.sig=K4F3faYzmVuqC0iXIERCQf55g2Y; xq_a_token=bac8e85c6e5546899703d83d4dbbc7a850644b17; xq_a_token.sig=_uufy5ObX9-ZcJIqCjvqjNAkMFY; xqat=bac8e85c6e5546899703d83d4dbbc7a850644b17; xqat.sig=yXpjV_WrUTNx3Qb2siG8WI5cVC0; xq_r_token=ed17b066bfb02fc424e072af82c5ad1f697b5c45; xq_r_token.sig=uAjUM3y3XlwWmxu1wxCipdcoy8o; xq_is_login=1; xq_is_login.sig=J3LxgPVPUzbBg3Kee_PquUfih7Q; u=4653766544; u.sig=l0rHkleae2sm4Ph3sS6R0e5K7f0; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1538957274; __utmb=1.2.10.1538957261'
    cookie2 = 'aliyungf_tc = AQAAAANRMQ1gJQ4AOBfIbxQAOI / p6dDF;xq_a_token = 06f8957ac1b238c9b71d432798fd6682d8cf9f6d;xq_r_token = 003285fd18aa96023ff92d68d7fa8cc51321aad4;Hm_lpvt_1db88642e346389874251b5a1eded6e3 = 1539587647;__utmc = 1;snbim_minify = true;xq_a_token.sig = X8orqO023a1mzBia94vXX5xkC4k;xq_token_expire = Fri % 20Nov % 2009 % 202018 % 2014 % 3A41 % 3A22 % 20GMT % 2B0800 % 20(CST);__utmz = 1.1539573787.1.1.utmcsr = (direct) | utmccn = (direct) | utmcmd = (none);xqat = 06f8957ac1b238c9b71d432798fd6682d8cf9f6d;remember.sig = K4F3faYzmVuqC0iXIERCQf55g2Y;bid = b0571b487a62d33e8a72fcdb418e7626_jn9xj3c7;device_id = 2cc9c53482d9994d81eb7d25e9d6aee9;remember = 1;__utma = 1.1094044701.1539568296.1539573787.1539584716.2;xqat.sig = amQQS - 1bn5tdsnBpE0rkBez6wpA;xq_r_token.sig = LM3IrbC_w7PyP764H0lMKrrsj50;u.sig = Xd_gEM - 0Lht6EixrnGkidNC - kHM;s = e012i39gwv;xq_is_login = 1;__utmb = 1.11.10.1539584716;u = 1925248886;_gid = GA1.2.287995963.1539568296;xq_is_login.sig = J3LxgPVPUzbBg3Kee_PquUfih7Q;_ga = GA1.2.1094044701.1539568296;Hm_lvt_1db88642e346389874251b5a1eded6e3 = 1539568202, 1539568236;__utmt = 1'
    if a == 0:
        cookie = cookie2
    elif a == 1:
        cookie = cookie1
    # print(symbol)
    symbol_split = symbol.split(".")
    symbol_xueqiu = symbol_split[1] + symbol_split[0]
    Data_xueqiu = getPoFriends(symbol_xueqiu, cookie1)

    Time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    if not (Data_xueqiu is None):
        return (Time, symbol, Data_xueqiu[2])


database = MongoDB.MongoDB("10.13.38.31", "27017")
stock_list = StockList(database)

for i in range(len(stock_list)):
    if i < 492:
        continue
    symbol = stock_list[i]
    followers = Data_Xueqiu_followers(symbol)
    if followers is None:
        print(i, "None")
        continue
    # 返回值为(更新时间, 股票代码, 关注者数量)
    print(i, followers)
    Time = followers[0]

    dirtyDT = Time.split(" ")
    dt = dirtyDT[0]
    localDateTime = Gadget.ParseDateTime(dt, "%Y-%m-%d")
    localDateTime = localDateTime + datetime.timedelta(hours=15)
    stdDateTime = Gadget.ToUTCDateTime(localDateTime)
    strDateTime = Gadget.ToDateTimeString(localDateTime)

    #
    followersDoc = {}
    followersDoc["Symbol"] = symbol
    followersDoc["StdDateTime"] = stdDateTime
    followersDoc["Followers"] = followers[2]
    followersDoc["Key"] = symbol + "_" + strDateTime
    followersDoc["Updatetime"] = Time

    # --- Insert Database---
    database.upsert("Text", "XueqiuFollowers", {"Key": followersDoc["Key"]}, followersDoc)

    # time.sleep(1)







